﻿using System;
using System.Threading;
using System.Text;
using System.Drawing;
using System.IO.Ports;
using System.Collections.Generic;

namespace ECMCommMan
{
    partial class ECMCommunicationManager
    {
        private byte[] bECMRequest;

        #region ECM Data Request
        /// <summary>
        /// method for sending Data Request the ECM
        /// </summary>
        /// <param name="receivedByte">byte array that contains Command Code in the 7th Byte</param>
        /// <returns>a byte in hex containing the Command code</returns>
        public void ECMDataRequest(byte bCommandType)
        {
            bECMRequest = new byte[9] { 0x01, 0x00, 0x42, 0x02, 0xff, 0x02, bCommandType, 0x03, 0x00 };

            bECMRequest[bECMRequest.Length - 1] = CalculateCheckSum(bECMRequest);

            WriteData(bECMRequest);
        }
        public void ECMDataRequest(byte bCommandType, byte bOffset, byte bPage, byte bData)
        {
            bECMRequest = new byte[12] {0x01, 0x00, 0x42, 0x05, 0xff, 0x02, bCommandType, bOffset, bPage, bData, 0x03, 0x00};
        
            bECMRequest[bECMRequest.Length - 1] = CalculateCheckSum(bECMRequest);

            WriteData(bECMRequest);
        }
        #endregion

        #region Analyse ECM Response
        /// <summary>
        /// method to find out what has been requested from the ECM
        /// </summary>
        /// <param name="receivedByte">byte array that contains Command Code in the 7th Byte</param>
        /// <returns>a byte in hex containing the Command code</returns>
        protected byte ECMAnalyseECMResponse(byte[] receivedByte)
        {
            if (receivedByte.Length > 7)
            {
                switch (receivedByte[6])
                {
                    case (byte)ECMCommandType.ECMGet:
                        m_parent.DisplayData(ECMCommMan.MessageType.Error, "ECMGet is not supported yet" + "\n");
                        break;
                    case (byte)ECMCommandType.ECMSet:
                        m_parent.DisplayData(ECMCommMan.MessageType.Error, "ECMSet is not supported yet" + "\n");
                        break;
                    case (byte)ECMCommandType.ECMVersion:
                        m_parent.DisplayData(ECMCommMan.MessageType.Outgoing, "Sending ECM Version Info\n");
                        ECMVersionNoResponse();
                        break;
                    case (byte)ECMCommandType.ECMRunTimeData:
                        m_parent.DisplayData(ECMCommMan.MessageType.Outgoing, "Sending ECM Run Time Data\n");
                        ECMRunTimeResponse();
                        break;
                    default:
                        m_parent.DisplayData(ECMCommMan.MessageType.Error, "Unknown message" + "\n");
                        break;
                }
                return receivedByte[6];
            }
            else
            {
                m_parent.DisplayData(ECMCommMan.MessageType.Error, "Dash Request not valid\n");
                return 0;
            }
        }
        #endregion

    }
}
